<?php
/**
 * 文件描述
 * Created on 2021/8/11 19:32
 * Create by LZH
 */

namespace App\Command;

use EasySwoole\Command\CommandManager;
use EasySwoole\Command\AbstractInterface\CommandHelpInterface;
use EasySwoole\Command\AbstractInterface\CommandInterface;
use EasySwoole\EasySwoole\Config;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\DbManager;

class Generate implements CommandInterface
{
    public function commandName(): string
    {
        return 'generate';
    }

    public function exec(): ?string
    {
        // 获取用户输入的命令参数
        $argv = CommandManager::getInstance()->getOriginArgv();

        if (count($argv) < 3) {
            echo "please input the action param!" . PHP_EOL;
            return null;
        }

        // remove generate
        array_shift($argv);

        // 获取 action 参数
        $action = $argv[1];

        // 下面就是对 自定义命令 的一些处理逻辑
        if (!$action) {
            echo "please input the action param!" . PHP_EOL;
            return null;
        }

        // 获取 option 参数
        $optionArr = $argv[2] ?? [];

        switch ($action) {
            case 'table':
                if ($optionArr) {
                    $strValue = explode('=', $optionArr);
                    if ($strValue[0] == '--tables') {
                        $tableNames = explode(',', $strValue[1]); // 多个表名
                    }
                    $tables = [];
                    foreach ($tableNames as $tableName) {
                        $table = [
                            'tableName' => $tableName,
                            'tableComment' => '自动生成表'
                        ];
                        $tables[] = $table;
                    }
                    // TODO 循环生成指定表的模块对象
                    $this->generateTables($tables);
                } else {
                    echo '请输入需要生成模块的表' . PHP_EOL;
                }
                break;
            case 'all':
                // TODO 循环生成所有表的模块对象
                $this->generateAll();
                break;
            default:
                echo "the action {$action} is not existed!" . PHP_EOL;
        }
        return null;
    }

    public function help(CommandHelpInterface $commandHelp): CommandHelpInterface
    {
        // 添加 自定义action(action 名称及描述)
        $commandHelp->addAction('table', '生成指定表的模块');
        $commandHelp->addAction('all', '生成所有表的模块');
        // 添加 自定义action 可选参数
        $commandHelp->addActionOpt('--tables=table1,table2,...', '输入需要生成模块的表名称');
        return $commandHelp;
    }

    // 设置自定义命令描述
    public function desc(): string
    {
        return '生成表模块的命令';
    }

    public function getTablesInfo(): array
    {
        $tables = [];

        $queryBuild = new QueryBuilder();
        $queryBuild->raw('show table status');
        $data = DbManager::getInstance()->query($queryBuild, true)->getResult();
        foreach ($data as $v) {
            $table = [
                'tableName' => $v["Name"],
                'tableComment'=> $v['Comment']
            ];
            $tables[] = $table;
        }
        return $tables;
    }

    private function generateTables($tables)
    {
        go(function () use ($tables) {
            $generateService = new GenerateService();
            $generateService->generate($tables);
        });
        // 清除全部定时器
        \Swoole\Timer::clearAll();
    }

    private function generateAll()
    {
        go(function () {
            $generateService = new GenerateService();
            $generateService->generate($this->getTablesInfo());
        });
        // 清除全部定时器
        \Swoole\Timer::clearAll();
    }
}
